
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>字符和字符串 - fubaisen</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="字符和字符串
在MATLAB中，对文本的处理一般是把其当作特征字符串或简单地当作字符串来处理的。字符串一般是用来构造命令，主要用于求值或者被执行。

如果我们在MATLAB中遇到对字符或者字符串的操,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="fubaisen" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="字符和字符串 - fubaisen"/>
    <meta name="twitter:description" content="字符和字符串
在MATLAB中，对文本的处理一般是把其当作特征字符串或简单地当作字符串来处理的。字符串一般是用来构造命令，主要用于求值或者被执行。

如果我们在MATLAB中遇到对字符或者字符串的操,"/>
    
    
    
    
    <meta property="og:site_name" content="fubaisen"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="字符和字符串 - fubaisen"/>
    <meta property="og:description" content="字符和字符串
在MATLAB中，对文本的处理一般是把其当作特征字符串或简单地当作字符串来处理的。字符串一般是用来构造命令，主要用于求值或者被执行。

如果我们在MATLAB中遇到对字符或者字符串的操,"/>
    
<link rel="stylesheet" href="/css/diaspora.css">

    <script>window.searchDbPath = "/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">fubaisen</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://fubaisen.github.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">字符和字符串</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">字符和字符串</h1>
        <div class="stuff">
            <span>三月 31, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/MATLAB%E5%AD%A6%E4%B9%A0/" rel="tag">MATLAB学习</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="字符和字符串"><a href="#字符和字符串" class="headerlink" title="字符和字符串"></a>字符和字符串</h1><blockquote>
<p>在<code>MATLAB</code>中，对文本的处理一般是把其当作特征字符串或简单地当作字符串来处理的。字符串一般是用来构造命令，主要用于求值或者被执行。</p>
</blockquote>
<p>如果我们在<code>MATLAB</code>中遇到对字符或者字符串的操作，那么<code>MATLAB</code>又是怎么处理的呢？</p>
<p>没错还是通过矩阵来处理，只不过处理的是字符内部的<code>ASCII</code>码，显示出来是字符而已，但是在内部储存的是基于<code>ASCII</code>数字的矩阵形式（包括向量）。既然字符串是通过矩阵来储存的，那我们就可以通过下标索引来进行访问。我们看如下例子</p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/09nZQwxgZ5v5OibyKhoXfEy74cNaSLM0WDY7Ep9sFyLMvOoB3thSWXic35tdibvp7nAvQumZjWpV7hg2FmT5sVROA/640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; String = <span class="string">&#x27;Welcome to use MATLAB&#x27;</span>;</span><br><span class="line">&gt;&gt; <span class="built_in">size</span>(String)</span><br><span class="line"></span><br><span class="line"><span class="built_in">ans</span> =</span><br><span class="line">     <span class="number">1</span>    <span class="number">21</span></span><br><span class="line">     </span><br><span class="line">&gt;&gt; whos</span><br><span class="line"></span><br><span class="line">  Name        Size            Bytes  Class     Attributes</span><br><span class="line">  String      <span class="number">1</span>x21               <span class="number">42</span>  char                </span><br><span class="line">  <span class="built_in">ans</span>         <span class="number">1</span>x2                <span class="number">16</span>  double              </span><br></pre></td></tr></table></figure>

<p>在<code>MATLAB</code>中，对字符串的储存是美国字符8个字节，如同<code>MATLAB</code>中的其他变量。下面检验<code>MATLAB</code>中的字符串是基于<code>ASCII</code>码表示的。</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; String = <span class="string">&#x27;Welcome to use MATLAB&#x27;</span>;</span><br><span class="line">&gt;&gt; U = <span class="built_in">abs</span>(String)</span><br><span class="line"></span><br><span class="line"><span class="comment">% 观其输出验得`MATLAB`中的字符串是基于`ASCII`码表示的</span></span><br><span class="line">U =</span><br><span class="line">  <span class="number">1</span> 至 <span class="number">10</span> 列</span><br><span class="line">    <span class="number">87</span>   <span class="number">101</span>   <span class="number">108</span>    <span class="number">99</span>   <span class="number">111</span>   <span class="number">109</span>   <span class="number">101</span>    <span class="number">32</span>   <span class="number">116</span>   <span class="number">111</span></span><br><span class="line">    </span><br><span class="line">  <span class="number">11</span> 至 <span class="number">20</span> 列</span><br><span class="line">    <span class="number">32</span>   <span class="number">117</span>   <span class="number">115</span>   <span class="number">101</span>    <span class="number">32</span>    <span class="number">77</span>    <span class="number">65</span>    <span class="number">84</span>    <span class="number">76</span>    <span class="number">65</span></span><br><span class="line">    </span><br><span class="line">  <span class="number">21</span> 列</span><br><span class="line">    <span class="number">66</span></span><br><span class="line">    </span><br><span class="line">&gt;&gt; U = U+<span class="number">0</span></span><br><span class="line"></span><br><span class="line">U =</span><br><span class="line">  <span class="number">1</span> 至 <span class="number">10</span> 列</span><br><span class="line">    <span class="number">87</span>   <span class="number">101</span>   <span class="number">108</span>    <span class="number">99</span>   <span class="number">111</span>   <span class="number">109</span>   <span class="number">101</span>    <span class="number">32</span>   <span class="number">116</span>   <span class="number">111</span></span><br><span class="line">    </span><br><span class="line">  <span class="number">11</span> 至 <span class="number">20</span> 列</span><br><span class="line">    <span class="number">32</span>   <span class="number">117</span>   <span class="number">115</span>   <span class="number">101</span>    <span class="number">32</span>    <span class="number">77</span>    <span class="number">65</span>    <span class="number">84</span>    <span class="number">76</span>    <span class="number">65</span></span><br><span class="line">    </span><br><span class="line">  <span class="number">21</span> 列</span><br><span class="line">    <span class="number">66</span></span><br></pre></td></tr></table></figure>

<p>字符串的索引示例：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; String = <span class="string">&#x27;Welcome to use MATLAB&#x27;</span>;</span><br><span class="line">&gt;&gt; U = String(<span class="number">7</span>:<span class="number">9</span>)        <span class="comment">% A(m,n):提取第m行，第n列元素</span></span><br><span class="line">U =</span><br><span class="line"></span><br><span class="line">    <span class="string">&#x27;e t&#x27;</span></span><br><span class="line">    </span><br><span class="line">&gt;&gt; A = String(<span class="number">10</span>:<span class="number">-1</span>:<span class="number">7</span>)      <span class="comment">%-1表示步长，从后往前取</span></span><br><span class="line">A =</span><br><span class="line"></span><br><span class="line">    <span class="string">&#x27;ot e&#x27;</span></span><br></pre></td></tr></table></figure>

<p>字符串的拼接与单引号问题：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt; String = <span class="string">&#x27;It&#x27;&#x27;s not the manual&#x27;</span>;</span><br><span class="line">&gt;&gt; String</span><br><span class="line">String =</span><br><span class="line">It&#x27;s not the manu</span><br><span class="line"></span><br><span class="line">&gt;&gt; U = <span class="string">&#x27;Hello,&#x27;</span>;</span><br><span class="line">&gt;&gt; V = <span class="string">&#x27;World!&#x27;</span>;</span><br><span class="line">&gt;&gt; W = [U V];</span><br><span class="line">&gt;&gt; W</span><br><span class="line">W =</span><br><span class="line">Hello,World!</span><br></pre></td></tr></table></figure>


            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
